<template>
    <el-card class="box-card" style="height: 380px; margin: 10px 0">
        <div slot="header" class="clearfix">
            <!--  @tab-click="handleClick" -->
            <!-- 头部左侧内容 -->
            <el-tabs class="tab" v-model="activeName">
                <el-tab-pane label="销售额" name="sale"></el-tab-pane>
                <el-tab-pane label="访问量" name="visite"></el-tab-pane>
            </el-tabs>
            <!-- 头部右侧内容 -->
            <div class="right">
                <span @click="setDay">今日</span>
                <span @click="setWeek">本周</span>
                <span @click="setMonth">本月</span>
                <span @click="setYear">本年</span>
                <el-date-picker v-model="date" class="date" type="daterange" range-separator="—"
                    start-placeholder="开始日期" end-placeholder="结束日期" size="mini" value-format="yyyy-MM-dd">
                </el-date-picker>
            </div>
        </div>
        <div>
            <el-row :gutter="10">
                <el-col :span="18">
                    <div class="charts" ref="charts"></div>
                </el-col>
                <el-col :span="6">
                    <h3 style="margin: 0">门店{{ title }}排行</h3>
                    <ul>
                        <li v-if="title=='销售额'" v-for="(shop,index) in chartsData.orderRank" :key="shop.no">
                            <span class="index" :class="shopCss(index)">{{shop.no}}</span>
                            <span class="shop">{{shop.name}}</span>
                            <span class="num"><strong class="flag">¥</strong>{{shop.money}}</span>
                        </li>
                        <li v-if="title=='访问量'" v-for="(user,index1) in chartsData.userRank" :key="user.no">
                            <span class="index" :class="shopCss(index1)">{{user.no}}</span>
                            <span class="shop">{{user.name}}</span>
                            <span class="num">{{user.money}}</span>
                        </li>
                    </ul>
                </el-col>
            </el-row>
        </div>
    </el-card>
</template>

<script>
import * as echarts from "echarts";
import * as dayjs from 'dayjs'
import chartsData from '../data.json'
export default {
    data() {
        return {
            activeName: "sale",
            myCharts: null,
            // 收集日历的数据
            date: [],
            chartsData: chartsData
        };
    },
    methods: {
        // 今日
        setDay() {
            const day = dayjs().format('YYYY-MM-DD');
            this.date = [day, day];
        },
        // 本周
        setWeek() {
            const start = dayjs().day(1).format('YYYY-MM-DD');
            const end = dayjs().day(7).format('YYYY-MM-DD');
            this.date = [start, end];
        },
        // 本月
        setMonth() {
            const start = dayjs().startOf('month').format('YYYY-MM-DD');
            const end = dayjs().endOf('month').format('YYYY-MM-DD');
            this.date = [start, end];
        },
        // 本年
        setYear() {
            const start = dayjs().startOf('year').format('YYYY-MM-DD');
            const end = dayjs().endOf('year').format('YYYY-MM-DD');
            this.date = [start, end];
        },
        // 门店排行样式
        shopCss(index) {
            if (index == 0) {
                return 'one'
            } else if (index == 1) {
                return 'two'
            } else if (index == 2) {
                return 'three'
            } else {
                return false
            }
        }
    },
    mounted() {
        // 初始化事件
        this.setDay();
        this.myCharts = echarts.init(this.$refs.charts);
        this.myCharts.setOption({
            title: {
                text: "销售额趋势",
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    type: "shadow",
                },
            },
            grid: {
                left: "3%",
                right: "4%",
                bottom: "3%",
                containLabel: true,
            },
            xAxis: [
                {
                    type: "category",
                    data: this.title == '销售额' ? this.chartsData.orderFullYearAxis : this.chartsData.userFullYearAxis,
                    axisTick: {
                        alignWithLabel: true,
                    },
                },
            ],
            yAxis: [
                {
                    type: "value",
                },
            ],
            series: [
                {
                    name: this.title == '销售额' ? '销售额' : '访问量',
                    type: "bar",
                    barWidth: "60%",
                    data: this.title == '销售额' ? this.chartsData.orderFullYear : this.chartsData.userFullYear,
                    color: "orange",
                },
            ]
        });
    },
    computed: {
        // 标题的计算属性
        title() {
            return this.activeName == "sale" ? "销售额" : "访问量";
        }
    },
    watch: {
        title() {
            this.myCharts.setOption({
                title: {
                    text: this.title + "趋势",
                },
                xAxis: [
                    {
                        data: this.title == '销售额' ? this.chartsData.orderFullYearAxis : this.chartsData.userFullYearAxis
                    },
                ],
                series: [
                    {
                        name: this.title == '销售额' ? '销售额' : '访问量',
                        data: this.title == '销售额' ? this.chartsData.orderFullYear : this.chartsData.userFullYear
                    }
                ]
            });
        },
    },
};
</script>

<style scoped>
.clearfix {
    display: flex;
    justify-content: space-between;
    position: relative;
}

.tab {
    width: 100%;
}

.right {
    position: absolute;
    right: 0;
}

.right>span {
    font-size: 15px;
    cursor: pointer;
}

.date {
    width: 250px;
    margin-left: 20px;
}

.right span {
    margin: 0 10px;
}

.charts {
    width: 100%;
    height: 250px;
}

ul {
    list-style: none;
    width: 100%;
    height: 250px;
    padding: 0;
}

ul li {
    height: 30px;
    display: flex;
    justify-content: space-between;
}

.index {
    width: 15px;
    height: 15px;
    display: inline-block;
    text-align: center;
    line-height: 15px;
    margin-right: 30px;
}

.one {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    display: inline-block;
    padding-top: 1px;
    font-size: 10px;
    color: white;
    text-align: center;
    line-height: 15px;
    background-color: #fe2d46;
}

.two {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    display: inline-block;
    padding-top: 1px;
    font-size: 10px;
    color: white;
    text-align: center;
    line-height: 15px;
    background-color: #f60;
}

.three {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    display: inline-block;
    padding-top: 1px;
    font-size: 10px;
    color: white;
    text-align: center;
    line-height: 15px;
    background-color: #faa90e;
}

.num {
    width: 80px;
    text-align: right;
}

.shop {
    flex: 1;
}
.flag {
    font-size: 15px;
    font-weight: 500;
    margin-right: 5px;
}
</style>
